home *** CD-ROM | disk | FTP | other *** search
- Path: rcp6.elan.af.mil!rscernix!danpop
- From: danpop@mail.cern.ch (Dan Pop)
- Newsgroups: comp.lang.c
- Subject: Re: argc/argv & switches
- Date: 29 Feb 96 11:25:42 GMT
- Organization: CERN European Lab for Particle Physics
- Message-ID: <danpop.825593142@rscernix>
- References: <4h2j8j$9gn@milo.freenet.vancouver.bc.ca>
- NNTP-Posting-Host: ues5.cern.ch
- X-Newsreader: NN version 6.5.0 #7 (NOV)
-
- zipz@opus.freenet.vancouver.bc.ca (Patrick Wong) writes:
-
- >I would like to know the difference between *++argv[0] and (*++argv)[0].
-
- This is a good exercise for someone trying to master complex C expressions
- but I strongly recommend against using such constructs in real code.
-
- *++argv[0] is equivalent to *(++(argv[0])), which means that argv[0] is
- incremented to point to the second character of the first program argument,
- then dereferenced, so the result of the expression is that character.
-
- (*++argv)[0] is equivalent to (*(++argv))[0], which means that argv is
- incremented to point to the second element of the argument array, then
- dereferenced by '*', to obtain a pointer to the second program argument,
- then dereferenced again (by [0]), to obtain the first character of the
- second program argument.
-
- So, if the program was invoked as
-
- myprog foo
-
- and argv[0] is "myprog" and argv[1] is "foo" (this statement is slightly
- inaccurate, argv[0] and argv[1] not being arrays, but pointers to the
- first character in an array), *++argv[0] will give 'y' (and argv[0] will
- be incremented) while (*++argv)[0] will give 'f' (and argv will be
- incremented).
-
- Note that *++argv[0] is technically incorrect, because there is no
- guarantee that argv[0] can be modified by the program, it can be legally
- stored in a read only memory segment.
-
- The table at page 53 of K&R2 will help understanding how these expressions
- are parsed.
-
- >Also, I've seen a lot of programs that have command-line switches like:
- >
- >view -r -x list.txt
- >view -rx list.txt
- >view -xr list.txt
- >
- >How can I write the view program so that it can accept all three formats?
-
- On a Unix platform, try "man 3 getopt", otherwise you can find a getopt
- implementation with archie or from the book "Obfuscated C And Other
- Misteries", by Don Libes.
-
- Dan
- --
- Dan Pop
- CERN, CN Division
- Email: danpop@mail.cern.ch
- Mail: CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
-